﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using eTerm.NetSocket.Utils;

namespace eTerm.NetSocket
{
    /// <summary>
    /// eTerm协议常量定义
    /// </summary>
    public static class __eTermPakcet
    {
        /// <summary>
        /// 登录认证名(用于字节替换)
        /// </summary>
        public static byte[] LogInPacket = new byte[] { 
                0x01,0xA2,0x4f,0x31,0x35,0x37,0x70,0x34,0x41,0x31,0x00,0x54,0x72,0x61,0x76,0x65,
                0x6c,0x53,0x41,0x41,0x50,0x50,0x00,0x3a,0x30,0x50,0x20,0x20,0x20,0x20,0x20,0x00,
                0x6d,0x61,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x33,0x37,0x33,
                0x36,0x33,0x31,0x33,0x31,0x30,0x00,0x30,0x30,0x30,0x30,0x30,0x30,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00
             };

        /// <summary>
        /// 连接维护包
        /// </summary>
        public static byte[] KeepLive = new byte[] { 
            0x01,0xFB,0x00,0x05,0x00 
        };


        /// <summary>
        /// 生成提示信息
        /// </summary>
        /// <param name="Message"></param>
        /// <returns></returns>
        public static byte[] BuildSessionPacket(string Message) {
            List<byte> __b = new List<byte>();
            __b.AddRange(new byte[] { 
                 0x01 
                ,0xF8 
                ,0x01 
                ,0xE2
            });
            __b.AddRange(GbToUsas(Message));
            __b.AddRange(new byte[] { 
                 0xC8
                ,0xD5
            });
            __b[1] = (byte)__b.Count;
            return __b.ToArray();
        }

        /// <summary>
        /// 通用提示生成.
        /// </summary>
        /// <param name="Rid">The Rid.</param>
        /// <param name="Sid">The Sid.</param>
        /// <param name="Message">提示内容.</param>
        /// <returns></returns>
        public static byte[] BuildSessionPacket(byte Rid, byte Sid, string Message)
        {
            List<byte> __b = new List<byte>();
            __b.AddRange(new byte[] { 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, Rid, Sid, 0x70, 0x02, 0x1B, 0x0B, 0x20, 0x21, 0x0F, 0x1B, 0x4D/*,0x1C*/ });
            StringBuilder sb = new StringBuilder();
            foreach (string s in Message.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
                sb.Append(s.PadRight(75, ' ')).Append("\r");

            foreach (byte b in GbToUsas(sb.ToString()))
            {
                if (b == 0x0A) continue;
                //if (b == 0x0D)
                //{
                //    __b.AddRange(new byte[] { 0x1D, 0x1C, 0x0D });
                //    continue;
                //}
                __b.Add(b);
            }
            //__b.AddRange(GbToUsas(Message));
            __b.AddRange(new byte[] {/*0x1D,*/ 0x1E, 0x1B, 0x62, 0x03 });
            byte[] lengthArray = BitConverter.GetBytes(ushort.Parse( __b.Count.ToString()));
            __b[2] = lengthArray[1];
            __b[3] = lengthArray[0];
            return __b.ToArray();
        }


        public static byte[] BuidAlertMessage(string message) {
            List<byte> rltBytes = new List<byte>() { 
                        0x01,0xF8,0x00,0x00
                    };
            rltBytes.AddRange(Encoding.Default.GetBytes(message));

            byte[] lengthAry = BitConverter.GetBytes(ushort.Parse(rltBytes.Count.ToString()));
            rltBytes[2] = lengthAry[1];
            rltBytes[3] = lengthAry[0];
            return rltBytes.ToArray();
        }


        /// <summary>
        /// 中文编码到Usas转换.
        /// </summary>
        /// <param name="inputString">输入字符串.</param>
        /// <returns></returns>
        private static byte[] GbToUsas(string inputString)
        {
            //StringBuilder sb = new StringBuilder(inputString);
            //foreach (Match m in Regex.Matches(inputString, @"[\u4e00-\u9fa5]+")) {
            //    sb.Replace(m.Value, string.Format(@"{0}",m.Value));
            //}

            byte[] org = Encoding.GetEncoding("gb2312").GetBytes(inputString);
            List<byte> result = new List<byte>();
            bool flag = false;
            foreach (byte b in org)
            {
                if (!flag && b > 128)
                {
                    result.AddRange(new byte[] { 0x1B, 0x0E });
                    flag = true;
                }
                else if (flag && b <= 128)
                {
                    result.AddRange(new byte[] { 0x1B, 0x0F });
                    flag = false;
                }
                if (flag)
                {
                    result.Add((byte)(b - 128));
                }
                else
                {
                    result.Add(b);
                }
            }
            if (flag)
                result.AddRange(new byte[] { 0x1B, 0x0F });
            return result.ToArray();
        }


        /// <summary>
        /// 第一个登录握手包(成功登录后)
        /// </summary>
        public static byte[] FIRST_LOGIN = new byte[] { 
           0x01,0xFE,0x00,0x11,0x14,0x10,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
        };



        /// <summary>
        /// 第二个登录握手包(成功登录后)
        /// </summary>
        public static byte[] SENCOND_LOGIN = new byte[] { 
           0x01,0xFE,0x00,0x11,0x14,0x10,0x00,0x02,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
        };


        /// <summary>
        /// 第三个登录握手包(成功登录后)
        /// </summary>
        public static byte[] THIRD_LOGIN = new byte[] { 
           0x01,0xFE,0x00,0x11,0x14,0x10,0x00,0x02,0x29,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
        };

        /// <summary>
        /// 提示信息读取包
        /// </summary>
        public static byte[] FOURTH_LOGIN = new byte[] { 
            0x01,0xF9,0x00,0x44,0x00,0x01,0x1B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
           ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
           ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
           ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 
           ,0x00,0x00,0x00,0x00
        };

        /// <summary>
        /// 登录欢迎信息包
        /// </summary>
        public static byte[] WELCOME_LOGIN = new byte[] { 
 0x01,0xF8,0x01,0xE2,0x65,0x54,0x65,0x72,0x6D,0xD3,0xC3,0xBB,0xA7,0xC4,0xFA,0xBA
,0xC3,0xA3,0xBA,0x0A,0x0A,0x20,0x20,0x20,0x20,0xCE,0xAA,0xC1,0xCB,0xCC,0xE1,0xB8
,0xDF,0x65,0x54,0x65,0x72,0x6D,0xCF,0xB5,0xCD,0xB3,0xD3,0xC3,0xBB,0xA7,0xD5,0xCA
,0xBB,0xA7,0xB5,0xC4,0xB0,0xB2,0xC8,0xAB,0xD0,0xD4,0xA3,0xAC,0xB1,0xA3,0xBB,0xA4
,0xB9,0xE3,0xB4,0xF3,0x65,0x54,0x65,0x72,0x6D,0xD3,0xC3,0xBB,0xA7,0xD5,0xCB,0xBA
,0xC5,0xB5,0xC4,0xCA,0xB9,0xD3,0xC3,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x0A,0xB0,0xB2,0xC8,0xAB,0xA3,0xAC,0xD6,0xD0,0xB9,0xFA,0xBA,0xBD,0xD0,0xC5,0xBD
,0xAB,0xB4,0xD3,0x39,0xD4,0xC2,0x31,0xC8,0xD5,0xBF,0xAA,0xCA,0xBC,0xB2,0xBB,0xD4
,0xD9,0xBD,0xD3,0xCA,0xDC,0xBC,0xF2,0xB5,0xA5,0xB5,0xC4,0x65,0x54,0x65,0x72,0x6D
,0xC3,0xDC,0xC2,0xEB,0xA1,0xA3,0xCB,0xF9,0xD3,0xD0,0x65,0x54,0x65,0x72,0x6D,0xD5
,0xCB,0xBA,0xC5,0x0A,0x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,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0xD6,0xD0,0xB9,0xFA,0xC3,0xF1,0xBA,0xBD,0xD0,0xC5,0xCF
,0xA2,0xCD,0xF8,0xC2,0xE7,0xB9,0xC9,0xB7,0xDD,0xD3,0xD0,0xCF,0xDE,0xB9,0xAB,0xCB
,0xBE,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0A,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
,0x20,0x20,0x20,0x20,0x20,0x32,0x30,0x31,0x31,0xC4,0xEA,0x37,0xD4,0xC2,0x32,0x30
,0xC8,0xD5
                             };





        #region 汉字编码
        /// <summary>
        /// Gets the bytes chinese.
        /// </summary>
        /// <param name="paramArrayOfByte">The param array of byte.</param>
        /// <returns></returns>
        public static byte[] getBytesChinese(byte[] paramArrayOfByte)
        {
            byte[] arrayOfByte = new byte[paramArrayOfByte.Length];

            int l = 2;
            for (int i1 = 0; i1 < paramArrayOfByte.Length; ++i1)
            {
                int i = (byte)(paramArrayOfByte[i1] & 0xF);
                if (l % 2 == 0)
                {
                    if (((byte)(paramArrayOfByte[i1] & 0xB0) == 176) && (i < 3))
                        arrayOfByte[i1] = (byte)(34 + i);
                    else
                        arrayOfByte[i1] = (byte)(paramArrayOfByte[i1] & 0x7F);
                }
                else
                {
                    int j = (byte)(paramArrayOfByte[i1] - 175);
                    if ((j < 4) && (j >= 0))
                    {
                        arrayOfByte[i1] = (byte)(37 + j);
                        int k = arrayOfByte[(i1 - 1)];
                        arrayOfByte[(i1 - 1)] = arrayOfByte[i1];
                        arrayOfByte[i1] = (byte)k;
                    }
                    else
                    {
                        arrayOfByte[i1] = (byte)(paramArrayOfByte[i1] & 0x7F);
                    }
                }
                ++l;
            }

            return arrayOfByte;
        }


        /// <summary>
        /// Gets the translate chinese.
        /// </summary>
        /// <param name="paramString">The param string.</param>
        /// <returns></returns>
        public static byte[] getTranslateChinese(String paramString)
        {
            List<byte> items = new List<byte>();
            try
            {
                byte[] arrayOfByte1 = Encoding.GetEncoding(@"gb2312").GetBytes(paramString);
                byte[] arrayOfByte2 = getBytesChinese(arrayOfByte1);

                items.AddRange(new byte[] { 0x1B, 0x0E });
                for (int i = 0; i < arrayOfByte2.Length; ++i)
                {
                    items.Add(arrayOfByte2[i]);
                }
                items.AddRange(new byte[] { 0x1B, 0x0F });
            }
            catch { }
            return items.ToArray();
        }

        /// <summary>
        /// Gets the chinese bytes.
        /// </summary>
        /// <param name="paramArrayOfByte">The param array of byte.</param>
        /// <returns></returns>
        public static byte[] getChineseBytes(byte[] paramArrayOfByte)
        {
            byte[] arrayOfByte = new byte[paramArrayOfByte.Length];

            for (int i = 0; i < paramArrayOfByte.Length; i += 2)
            {
                if ((paramArrayOfByte[i] <= 40) && (paramArrayOfByte[i] >= 37))
                {
                    arrayOfByte[1] = (byte)(paramArrayOfByte[i] - 37 + 175);
                    if ((paramArrayOfByte[(i + 1)] <= 36) && (paramArrayOfByte[(i + 1)] >= 34))
                        arrayOfByte[0] = (byte)(paramArrayOfByte[(i + 1)] - 34 + 176);
                    else
                    {
                        arrayOfByte[0] = (byte)(paramArrayOfByte[(i + 1)] | 0x80);
                    }
                }
                else
                {
                    arrayOfByte[1] = (byte)(paramArrayOfByte[(i + 1)] | 0x80);
                    if ((paramArrayOfByte[i] <= 36) && (paramArrayOfByte[i] >= 34))
                        arrayOfByte[0] = (byte)(paramArrayOfByte[i] - 34 + 176);
                    else
                    {
                        arrayOfByte[0] = (byte)(paramArrayOfByte[i] | 0x80);
                    }
                }
            }

            return arrayOfByte;
        }

        /// <summary>
        /// 中文编码到Usas转换.
        /// </summary>
        /// <param name="inputString">输入字符串.</param>
        /// <returns></returns>
        public static byte[] getTranslateToeTermChinese(string inputString)
        {
            StringBuilder sb = new StringBuilder(inputString);
            foreach (Match m in Regex.Matches(inputString, @"[\u4e00-\u9fa5]+"))
            {
                sb.Replace(m.Value, string.Format(@"{0}{1}", Cn2PyUtil.FullConvert(m.Value), Encoding.GetEncoding("gb2312").GetString(getTranslateChinese(m.Value))));
            }
            return Encoding.GetEncoding("gb2312").GetBytes(sb.ToString());
        }
        #endregion
    }
}
